import go from 'gojs'
import config from '../../../config'
import Util from '../../../util'
import { makeFont } from '@/utils'
// import { linkMouseEnter, linkMouseLeave } from './mouse'

// 直线分支可以像多段线一样进行编辑!!!
// file:///D:/develop/GitTest/GoJS/extensions/PolylineLinking.html
export default function() {
  const $$ = go.GraphObject.make // 后面很多用到该变量来初始化diagram

  return $$(
    go.Link, // the whole link panel
    {
      routing: go.Link.Normal,
      curve: go.Link.None,
      toShortLength: 0,
      reshapable: true,
      resegmentable: true, // 允许分段
      relinkableFrom: true,
      relinkableTo: true,
      adjusting: go.Link.Stretch
      // 分支高亮效果
      // mouseEnter: (e, obj) => linkMouseEnter(e, obj),
      // mouseLeave: (e, obj) => linkMouseLeave(e, obj)
    },
    new go.Binding('points', 'line_points', Util.Go.toPoints).makeTwoWay(
      Util.Go.fromPoints
    ),
    new go.Binding('routing', 'ortho', Util.Go.toOrthogonal),
    // { toShortLength: 5 }, // shortens path to avoid interfering with arrowhead
    $$(
      go.Shape,
      {
        isPanelMain: true,
        name: 'EDGE_SHAPE',
        stroke: config.edge.style.stroke, // 分支线条颜色
        strokeWidth: config.edge.style.strokeWidth // 分支线宽
      },
      new go.Binding('stroke', 'stroke').makeTwoWay(),
      new go.Binding('strokeWidth', 'strokeWidth').makeTwoWay()
    ),
    // $$(
    //   go.Shape,
    //   { isPanelMain: true, stroke: config.edge.style.stroke, strokeWidth: 2 },
    //   new go.Binding('stroke', 'stroke').makeTwoWay()
    // ),
    // 增加一个动画shape
    $$(go.Shape, {
      isPanelMain: true,
      stroke: 'white',
      strokeWidth: 1.5,
      name: 'AIRFLOW',
      // TODO 动画中白色移动块的长度和间距(应根据风速动态调整)
      strokeDashArray: [10, 10]
    }),
    // 自定义箭头: 居中,拉长
    // $$(go.Shape, { toArrow: 'standard', stroke: null, scale: 1 }),
    $$(
      go.Shape,
      {
        name: 'EDGE_ARROW',
        // 标准箭头
        // toArrow: 'Standard',
        // 自定义箭头: 箭头在分支中间
        toArrow: 'Triangle',
        segmentIndex: -Infinity,
        geometry: go.Geometry.parse('F1 m 0,0 l 16,4 -16,4 z'),
        stroke: config.edge.style.stroke,
        fill: config.edge.style.stroke
      },
      new go.Binding('stroke', 'stroke').makeTwoWay(),
      new go.Binding('fill', 'stroke').makeTwoWay()
    ),
    $$(
      go.TextBlock, // the label text
      {
        // cursor: 'move',
        name: 'EDGE_TEXT',
        segmentOffset: new go.Point(0, -10),
        segmentOrientation: go.Link.OrientUpright,
        textAlign: 'center',
        font: makeFont(config.edge.style.fontSize, false),
        stroke: config.edge.style.stroke,
        margin: 0,
        editable: false // enable in-place editing
      },
      new go.Binding('text').makeTwoWay(),
      // new go.Binding('font').makeTwoWay(),
      new go.Binding('font', 'fontSize', Util.Go.toFont).makeTwoWay(
        Util.Go.fromFont
      ),
      new go.Binding('stroke', 'fontColor').makeTwoWay(),
      // The GraphObject.segmentOffset property is what the LinkLabelDraggingTool modifies.
      // This TwoWay binding saves any changes to the same named property on the link data.
      new go.Binding(
        'segmentOffset',
        'segmentOffset',
        go.Point.parse
      ).makeTwoWay(go.Point.stringify)
    ),
    $$(
      go.TextBlock,
      {
        name: 'EDGE_TEXT',
        segmentOffset: new go.Point(0, 18),
        segmentOrientation: go.Link.OrientUpright,
        isMultiline: true,
        editable: false
      },
      new go.Binding('text', 'text2').makeTwoWay()
    )
  )
}
